package com.pengweiwei.androidzygote.functions.base;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.annotations.SerializedName;
import com.pengweiwei.androidzygote.R;
import com.putong35.common.baseactfrag.activity.PullableListActivity;
import com.putong35.common.baseactfrag.adapter.RecyclerViewBaseAdapter;
import com.putong35.common.httprequest.Response;
import com.putong35.common.utils.EasyToast;
import com.putong35.common.utils.io.AssetsUtil;

import java.util.List;

import static com.putong35.common.baseactfrag.adapter.RecyclerViewBaseAdapter.ITEM_VIEW_TYPE_ITEM;


/**
 */
public class MultiTypeActivity extends PullableListActivity {

    private int mPage = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
        TextView textView = new TextView(this);
        textView.setText("header");
        textView.setPadding(50, 50, 50, 50);
        getAdapter().setHeaderView(textView);
    }

    private void init() {
        mPage = 0;
        clearDecorations();
        setPullToRefreshEnable(false);
    }

    @Override
    public void onResume() {
        super.onResume();
        startLoading();
    }

//    private ListBeanResponseListener<TemplateBean> mResponceListener = new ListBeanResponseListener<TemplateBean>(this, TemplateBean.class);

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new TemplateListAdapter(this);
    }


    @Override
    public void onStartLoading() {
        String responseStr = AssetsUtil.getStringFromAssets(this, "list_ui_test_data.txt");
        Response parser = new Response(responseStr);
        List<TemplateBean> list = parser.getDataListBean(TemplateBean.class);
        if (list != null && list.size() > 0) {
            getAdapter().getData().addAll(list);
        }

    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long itemId) {
        EasyToast.showToast(this, "posi = " + position);
    }

    public static final int TYPE_1 = ITEM_VIEW_TYPE_ITEM | 1;
    public static final int TYPE_2 = ITEM_VIEW_TYPE_ITEM | 2;

    /**
     * Adapter
     */
    public class TemplateListAdapter extends RecyclerViewBaseAdapter<TemplateBean> {

        public TemplateListAdapter(Context con) {
            super(con);
        }

        @Override
        public RecyclerView.ViewHolder onCreateHolder(ViewGroup viewGroup, int type) {

            if (type == TYPE_1) {
                Log.e("pww", "onCreateHolder: item TYPE_1 = ");
                return new ItemViewHolder(setAdapterViewItem(viewGroup, R.layout.listitem_template1));
            }
            Log.e("pww", "onCreateHolder: item type = default ");
            return new ItemViewHolder(setAdapterViewItem(viewGroup, R.layout.listitem_template));
        }


        @Override
        public void onBindHolder(RecyclerView.ViewHolder viewHolder, int i) {
            ItemViewHolder holder = (ItemViewHolder) viewHolder;
            final TemplateBean bean = getData().get(i);
//            ImageLoader.with(RequestDemoListActivity.this).load(bean.getIconUrl()).into(holder.ivIcon).start();
            holder.tvText.setText(bean.getText());
        }

        public class ItemViewHolder extends RecyclerView.ViewHolder {
            public RelativeLayout lytMain;
            public TextView tvText;
            public ImageView ivIcon;

            public ItemViewHolder(View itemView) {
                super(itemView);
                lytMain = (RelativeLayout) itemView.findViewById(R.id.lytMain);
                ivIcon = (ImageView) itemView.findViewById(R.id.ivIcon);
                tvText = (TextView) itemView.findViewById(R.id.tvText);
            }
        }

        @Override
        public int getItemViewType(int position) {
            int superType = super.getItemViewType(position);
            if (superType == ITEM_VIEW_TYPE_ITEM) {
                if (position % 2 == 0) {
                    Log.e("pww", "getItemViewType:  TYPE_1 position = "+ position );
                    return TYPE_1;
                } else {
                    Log.e("pww", "getItemViewType:  ITEM_VIEW_TYPE_ITEM position = "+ position );
                    return ITEM_VIEW_TYPE_ITEM;
                }
            } else {
                return superType;
            }

        }
    }


    /**
     * 数据对象 Bean
     */
    public static class TemplateBean {
        @SerializedName("icoUrl")
        String icoUrl;

        String name;

        public String getIconUrl() {
            return icoUrl;
        }

        public void setIconUrl(String iconUrl) {
            this.icoUrl = iconUrl;
        }

        public String getText() {
            return name;
        }

        public void setText(String text) {
            this.name = text;
        }
    }

}
